import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

export interface IData<T> {
  data: T;
}

@Injectable()
export class ResponseInterceptor<T> implements NestInterceptor<T, IData<T>> {
  intercept(
    context: ExecutionContext,
    next: CallHandler<any>,
  ): Observable<IData<T>> | Promise<Observable<IData<T>>> {
    return next.handle().pipe(
      map((data) => ({
        data,
        status: 200,
        success: true,
        message: 'success',
      })),
    );
  }
}
